package demo2;

import demo1.Book;
import demo1.BookList;

import java.util.Scanner;

public class AddOperation implements IOoperation{

    @Override
    public void work(BookList bookList) {
//1.判断书架是否放满
        int currentSize=bookList.getUseSize();
        if(currentSize==bookList.getBook().length){
            System.out.println("书架已放满");
            return;
        }
//2.获取用户输入
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入书名：");
        String name=scanner.nextLine();
        System.out.println("请输入作者：");
        String author=scanner.nextLine();

        System.out.println("请输入类型");
        String type=scanner.nextLine();
        System.out.println("请输入价格：");
        int price=scanner.nextInt();
//3.创建一本书
        Book newbook=new Book(name,author,price,type);
//4.判断书架是否有这本书
        for (int i = 0; i < currentSize; i++) {
            Book book=bookList.getBook(i);
            if(book.getName().equals(name)){
                System.out.println("书架已存在这本书");
                return;
            }
        }
//5.将这本书放入书架
        bookList.setBook(currentSize,newbook);
        bookList.setUseSize(currentSize+1);
        System.out.println("添加成功");
    }
}
